
const mongoose = require('mongoose');
var express = require('express')
// 注意这里的声明
var titles = express()
const {tittleService} = require('../../service/impl/tittleService/tittleService');
const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const ObjectId = mongoose.Types.ObjectId;





//#region  all题目
/* 发布题目 */
// 这里是过去的API,全部采用新的API
// router.post('/post/titles/title', controller.contittle.caddtittle)
// /* 查找题目 */
// router.get('/get/tittles/tittle', controller.contittle.cfindtittle)
// /* 查询有限数目的题目 */
// router.get('/get/tittles/tittle50', controller.contittle.findtittle50)


/* 添加题目 */
titles.post('/post/tittle/add', async (req,res) => {
  let result
  try {
      let news = req.body.news
      result = await tittleService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })


  /* 删除题目 */
  titles.post('/post/tittle/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await tittleService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })


  /* 修改题目 */
  titles.post('/post/tittle/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await tittleService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  // 修改题目的审核状态
  titles.post('/post/tittle/updateAccess', async (req,res) => {
    console.log(req.body,1111);
    let result
    try {
      let  id  = req.body.conditions._id
      let updates = req.body.updates
      result =  await tittleService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  

  /* 查询所有题目 */
  titles.post('/post/tittle/queryAll', async (req,res) => {
    let result
    let fills ={
      "key1":{
        "path":"kemuid"
      },
      "key3":{
        "path":"xuekeid"
      },
      "key4":{
        "path":"point"
      }
    }
    try {
      result =  await tittleService.getIns().find(null,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  // 根据科目id,查询包含的已过审的题目
  // titles.post('/post/tittle/queryTitlesByKemuid', async (req,res) => {
  //           let result
  //           try {
  //             let conditions  = req.body.conditions
  //             result =  await tittleService.getIns().find(conditions)
  //           } catch (error) {
  //             console.log(error);
  //             res.send(returnDatas(false,null,error))
  //             return
  //           }
  //           res.send(returnDatas(true,result))
  // })
  
  // I09-根据科目id,查询该科目包含的题目(已过审,1,未过审0)--管理页面使用
  titles.post('/post/tittle/query_titles_by_kemuID', async (req,res) => {
    let result
    let fills ={
      "key1":{
        "path":"typeNumber"
      },
      "key2":{
        "path":"point"
      }
    }
    try {
      let  conditions = req.body.conditions
      result =  await tittleService.getIns().find(conditions,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })


  /* 客户端使用 -根据科目id,查询该科目包含的题目,分页查询*/
  titles.post('/post/tittle/queryTitlesByKemuIDClient', async (req,res) => {
    let result
    console.log(req.body,333)
    let fills ={
      // "key1":{
      //   "path":"typeNumber"
      // },
      "key2":{
        "path":"point"
      }
    }
    let hides={
      "jiaoyan":0,
      "shenhe":0
    }
    try {
      let  conditions 
      if("point" in req.body.conditions){
        conditions  = {
          "shenhe":1,
          "kemuid":req.body.conditions.kemuid,
          "point":req.body.conditions.point
        }
      }else {
        conditions  = {
          "shenhe":1,
          "kemuid":req.body.conditions.kemuid,
        }
      }
      //
      let currentPage = req.body.conditions.currentPage
      // 每页数量
      let pageSize = 20
      let skip = (currentPage-1)*pageSize

      result =  await tittleService.getIns().find(conditions,hides,fills,skip,pageSize)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })

  
  // I08-根据科目id,查询对应的待审题目,未经过审题
  titles.post('/post/tittle/queryTitlesByKemuidandUnok', (req,res) => {
    // controller.tittleController.getSigleIns().findByObj(req,res)
  
  })
  
  // I09-根据科目id,查看已校验,已审核的题目
  titles.post('/post/tittle/queryjiaoyanOkshenheok', (req,res) => {
   // controller.tittleController.getSigleIns().findByObj(req,res)
  })
  
  
  
  // E04-根据用户id查看该用户发布的所有题目
  titles.post('/post/tittle/user_titles', (req,res) => {
   // controller.tittleController.getSigleIns().findByObj(req,res)
  })
  
  
  // E05-用户删除自己已发布的题目
  titles.post('/post/tittle/delete_user_title', (req,res) => {
   // controller.tittleController.getSigleIns().findByIdAndRemove(req,res)
  })
  
  // E06-用户修改自己发布的题目
  titles.post('/post/tittle/update/user_self_title', (req,res) => {
   // controller.tittleController.getSigleIns().findByObjAndUpdate(req,res)
  })
  
  // E07-根据用户id和职业id,查看该用户在该职业发布的题目
  titles.post('/post/tittle/query_user_title_by_profession', (req,res) => {
  //  controller.tittleController.getSigleIns().findByObj(req,res)
  })
  
  // E08-根据科目id和用户id,用户查看自己发布的指定科目的题目
  titles.post('/post/tittle/query_user_title_by_userid_kemuid', (req,res) => {
  //  controller.tittleController.getSigleIns().findByObj(req,res)
  })
  
  // I07-根据职业id查询对应的待审题目,还没有审题的那些题目
  // router.post('/post/tittle/unok/query_useserid_kemuid', (req,res) => {
  //   controller.tittleController.getSigleIns().findByObj(req,res)
  // })
  
  
  
  
  
  
  //#endregion
  

  module.exports= titles